package ModStats;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DoCalculs {
	static private String fileDataName;
	
	public static void setFileName(String fName) {
		fileDataName = fName;
	}
	
	public static String getFileName() {
		return fileDataName;
	}
	
	public static List<InputObject> getValues() throws Exception {
		List<InputObject> lt = new ArrayList<InputObject>();
		ReadData rd = new ReadData(fileDataName);
		
		
		//set and get data
		rd.setData();
		lt = rd.getData();
				
		//System.out.println("Values :"+values.size());
		return lt;
	}
	
	public static String getMeans() throws Exception {
		
		List<InputObject> lt = DoCalculs.getValues();
		String result = "";// = new String();
		//get values
		ValuesList values = new ValuesList();
		BufferedReader ReadIn = null;
		
		try {
			 ReadIn = new BufferedReader(
					new FileReader(fileDataName));
		} catch (FileNotFoundException e) {
			System.out.println("File Not found !");
			e.printStackTrace();
		}
		
		//get all values
		for(int k = 0; k < FilesPropreties.getNbOfColumns(ReadIn)-1;k++) {
			for(int i = 0; i < lt.size(); i++) {
				for(int j = 0; j < lt.get(i).getValuesList(k).size(); j++) {
					values.add(lt.get(i).getValuesList(k).get(j));
				}
			}
			result = result.concat("Column "+(k+1)+" : "+CalculStats.mean(values)+"\n");
			values.clear();
		}		
		return result;
	}
	
	public static String getMinMax() throws Exception {
		List<InputObject> lt = DoCalculs.getValues();
		String result = "";// = new String();
		//get values
		ValuesList values = new ValuesList();
		BufferedReader ReadIn = null;
		
		try {
			 ReadIn = new BufferedReader(
					new FileReader(fileDataName));
		} catch (FileNotFoundException e) {
			System.out.println("File Not found !");
			e.printStackTrace();
		}
		
		//get all values
		for(int k = 0; k < FilesPropreties.getNbOfColumns(ReadIn)-1;k++) {
			for(int i = 0; i < lt.size(); i++) {
				for(int j = 0; j < lt.get(i).getValuesList(k).size(); j++) {
					values.add(lt.get(i).getValuesList(k).get(j));
				}
			}
			result = result.concat("Column "+(k+1)+" : min = "+CalculStats.min(values)+" and max = "+CalculStats.max(values)+"\n");
			values.clear();
		}		
		return result;
	}
	
	
	public static String getMedian() throws Exception {
		List<InputObject> lt = DoCalculs.getValues();
		String result = "";// = new String();
		//get values
		ValuesList values = new ValuesList();
		BufferedReader ReadIn = null;
		
		try {
			 ReadIn = new BufferedReader(
					new FileReader(fileDataName));
		} catch (FileNotFoundException e) {
			System.out.println("File Not found !");
			e.printStackTrace();
		}
		
		//get all values
		for(int k = 0; k < FilesPropreties.getNbOfColumns(ReadIn)-1;k++) {
			for(int i = 0; i < lt.size(); i++) {
				for(int j = 0; j < lt.get(i).getValuesList(k).size(); j++) {
					values.add(lt.get(i).getValuesList(k).get(j));
				}
			}
			result = result.concat("Column "+(k+1)+" : "+CalculStats.median(values)+"\n");
			values.clear();
		}		
		return result;
	}
	
	
	public static String getVariance() throws Exception {
		List<InputObject> lt = DoCalculs.getValues();
		String result = "";// = new String();
		//get values
		ValuesList values = new ValuesList();
		BufferedReader ReadIn = null;
		
		try {
			 ReadIn = new BufferedReader(
					new FileReader(fileDataName));
		} catch (FileNotFoundException e) {
			System.out.println("File Not found !");
			e.printStackTrace();
		}
		
		//get all values
		for(int k = 0; k < FilesPropreties.getNbOfColumns(ReadIn)-1;k++) {
			for(int i = 0; i < lt.size(); i++) {
				for(int j = 0; j < lt.get(i).getValuesList(k).size(); j++) {
					values.add(lt.get(i).getValuesList(k).get(j));
				}
			}
			result = result.concat("Column "+(k+1)+" : "+CalculStats.variance(values)+"\n");
			values.clear();
		}		
		return result;
	}
	
	public static String getStandDeviation() throws Exception {
		List<InputObject> lt = DoCalculs.getValues();
		String result = "";// = new String();
		//get values
		ValuesList values = new ValuesList();
		BufferedReader ReadIn = null;
		
		try {
			 ReadIn = new BufferedReader(
					new FileReader(fileDataName));
		} catch (FileNotFoundException e) {
			System.out.println("File Not found !");
			e.printStackTrace();
		}
		
		//get all values
		for(int k = 0; k < FilesPropreties.getNbOfColumns(ReadIn)-1;k++) {
			for(int i = 0; i < lt.size(); i++) {
				for(int j = 0; j < lt.get(i).getValuesList(k).size(); j++) {
					values.add(lt.get(i).getValuesList(k).get(j));
				}
			}
			result = result.concat("Column "+(k+1)+" : "+CalculStats.standDeviation(values)+"\n");
			values.clear();
		}		
		return result;
	}
	
	public static String getAll() throws Exception {
		List<InputObject> lt = DoCalculs.getValues();
		String result = "";// = new String();
		//get values
		ValuesList values = new ValuesList();
		BufferedReader ReadIn = null;
		result = result.concat("Col. \t  Mean\t Min\t Max\t Median\t StDev\t Variance \n\n");
		try {
			 ReadIn = new BufferedReader(
					new FileReader(fileDataName));
		} catch (FileNotFoundException e) {
			System.out.println("File Not found !");
			e.printStackTrace();
		}
		
		//get all values
		for(int k = 0; k < FilesPropreties.getNbOfColumns(ReadIn)-1;k++) {
			for(int i = 0; i < lt.size(); i++) {
				for(int j = 0; j < lt.get(i).getValuesList(k).size(); j++) {
					values.add(lt.get(i).getValuesList(k).get(j));
				}
			}
			result = result.concat((k+1)+".\t"+CalculStats.mean(values)+"\t"+CalculStats.min(values)+"\t"+CalculStats.max(values)+"\t"+CalculStats.median(values)+"\t"+CalculStats.standDeviation(values)+"\t"+CalculStats.variance(values)+"\n");
			values.clear();
		}		
		return result;
	}
}
